var apiHost = wx.getStorageSync('apiHost');
var yunId = wx.getStorageSync('yunId'); 
var openId = wx.getStorageSync('openId'); 
var deliveryAreaType = "";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    xian: deliveryAreaType,
    condition: false,
    quyu:'添加区域',
    showModal: false,
    showModaldel: false,
    showModalgai: false,
    area: []
  },
  onLoad: function (options) {
    // deliveryAreaType = options.deliveryAreaType
    // this.switch(e);
    this.aa()
  },
  switch: function (e) {
  console.log(e.detail.value)
  var condition = e.detail.value
  if (condition == false){
    deliveryAreaType = '0';
   this.setData({
     xian: "不受限",
     condition: false
  })
  }else{
    deliveryAreaType = '1';
    this.setData({
      xian: "受限",
      condition: true,
      quyu:"添加区域"
    })
  }
  this.backDeliveryAreaType(deliveryAreaType);
  },
  //此方法用于文本框输入回调
  backDeliveryAreaType: function (deliveryAreaType) {
    // console.log(deliveryAreaType);
    //获取页面栈
    var pages = getCurrentPages();
    if (pages.length > 1) {
      //上一个页面实例对象
      var prePage = pages[pages.length - 2];
      //关键在这里
      prePage.changeDeliveryAreaType(deliveryAreaType)
    }
  },
  // 添加地址
  showDialogBtn: function (e) {
    var that = this;
    that.setData({
      showModal: true,
      quyu:"新建"
    })

  },
  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMove: function () {
  },
  /**
   * 隐藏模态对话框
   */
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function () {
    this.hideModal();
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
   
  
    var that = this;
    var name = that.data.namee;
    // console.log(name)
    wx.request({
      url: apiHost + "/xcx/deliveryArea/addOrUpdate",
      data: {
        yunId: yun,
        openId:openId,
        name: name
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        that.hideModal();
        that.aa();   //调用aa
      }
    })

  },
  bindKeyInput2:function(e){
    this.setData({
      namee: e.detail.value
    })
  },




  // 删除地址
  showDialogBtndel: function (e) {
    var that = this;
    var areaId = e.target.dataset.toggle;
    console.log(areaId)
    that.setData({
      showModaldel: true,
      areaId: areaId
    })
  },
  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMovedel: function () {
  },
  /**
   * 隐藏模态对话框
   */
  hideModaldel: function () {
    this.setData({
      showModaldel: false
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCanceldel: function () {
    this.hideModaldel();

  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirmdel: function () {
    
    var that = this
    var areaId = that.data.areaId
    // console.log(areaId)
    wx.request({
      url: apiHost + "/xcx/deliveryArea/del",
      data:{
        yunId: yunId,
        openId: openId,
        areaId: areaId
      },
      header:{
        "Content-Type": "application/json"
      },
      success:function(res){
        that.hideModaldel();
        that.aa();
      }
    })
    
  },






  // 修改地址
  showDialogBtngai: function (e) {
    var that = this;
    var bianjidizhi = e.target.dataset.toggle;//获取的拼接
    var bianjidizhiList = bianjidizhi.split(",");//以逗号截取
    var name = bianjidizhiList[0];
    var areaId = bianjidizhiList[1];
    console.log(areaId)
    console.log(name)
    that.setData({
      showModalgai: true,
      name:name,
      areaId: areaId
    })
  },
  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMovegai: function () {
  },
  /**
   * 隐藏模态对话框
   */
  hideModalgai: function () {
    this.setData({
      showModalgai: false
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCancelgai: function () {
    this.hideModalgai();
  },
  /**
   * 对话框确认按钮点击事件
   */
  
  onConfirmgai: function () {
    
    var that = this;
    var name = that.data.name;
    var areaId = that.data.areaId
    // console.log(name);

    wx.request({

      url: apiHost + "/xcx/deliveryArea/addOrUpdate",
      data:{
        yunId: yunId,
        openId: openId,
        name: name,
        areaId: areaId
      },
      header:{
        "Content-Type": "application/json"
      },
      success:function(res){
        that.hideModalgai();
        that.aa()
      }
    })

    

  },
  bindKeyInput:function(e){
    this.setData({
      name: e.detail.value
    })
  },
  
 aa:function(){
   var _this = this
    wx.request({
      url: apiHost + "/xcx/deliveryArea/list",
      data: {
        yunId: yunId,
        openId: openId
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg

        if (code == "10000") {
          _this.setData({
            area: res.data.data
          })
        }else{
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000,
            mask: true
          });
        }

      }
    })
  }
  // /**
  //  * 生命周期函数--监听页面加载
  //  */
  // onLoad: function (options) {
    
  // },

  // /**
  //  * 生命周期函数--监听页面初次渲染完成
  //  */
  // onReady: function () {
    
  // },

  // /**
  //  * 生命周期函数--监听页面显示
  //  */
  // onShow: function () {
    
  // },

  // /**
  //  * 生命周期函数--监听页面隐藏
  //  */
  // onHide: function () {
    
  // },

  // /**
  //  * 生命周期函数--监听页面卸载
  //  */
  // onUnload: function () {
    
  // },

  // /**
  //  * 页面相关事件处理函数--监听用户下拉动作
  //  */
  // onPullDownRefresh: function () {
    
  // },

  // /**
  //  * 页面上拉触底事件的处理函数
  //  */
  // onReachBottom: function () {
    
  // },

  // /**
  //  * 用户点击右上角分享
  //  */
  // onShareAppMessage: function () {
    
  // }
})